package com.zmx.doctor.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.R;
import com.zmx.doctor.adapter.SearchArticleAdapter;
import com.zmx.doctor.okhttp.OkHttp3ClientManager;
import com.zmx.doctor.pojo.SearchArticle;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DiseaseSearchActivity extends BaseActivity {

    @BindView(R.id.registered)
    TextView registered;
    @BindView(R.id.registered_text)
    EditText registered_text;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private SearchArticleAdapter adapter;
    private List<SearchArticle> lists;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_disease_search;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new SearchArticleAdapter(mActivity);
        recyclerView.setAdapter(adapter);
        lists = new ArrayList<>();

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Intent intent = new Intent();
                intent.putExtra("id",lists.get(listPosition).getId());
                intent.setClass(mActivity, TitleMessageActivity.class);
                mActivity.startActivity(intent);
            }
        });

    }

    @OnClick(R.id.registered)
    public void onViewClicked() {

        if (registered_text.getText().toString().equals("")){

            Toast("请输入内容");
            return;

        }

        getInfo(registered.getText().toString());

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    try {

                        lists.clear();
                        JSONObject object = new JSONObject(msg.obj.toString());
                        Gson gson = new Gson();
                        JSONArray data = object.getJSONArray("data");
                        for (int i = 0;i<data.length();i++){

                            SearchArticle sa = gson.fromJson(data.getJSONObject(i).toString(),SearchArticle.class);
                            lists.add(sa);

                        }

                        adapter.setListAndNotifyDataSetChanged(lists);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

            }

        }
    };

    public void getInfo(String keyword) {

        List<String> ls = new ArrayList<>();
        ls.add(keyword);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "search");
        params.put("data[keyword]", keyword);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/article", params, handler, 1, 404);

    }



}

